﻿// Copyright (C) 2012 Kim Jensen, TaskJunction.com
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaskJunctionLib.Model;

namespace TaskJunctionLib.Mapping.JSON
{
    public class IterationMapper : JSONMapper<Iteration>
    {
        public override void deserializeFromJSON(System.Json.JsonValue jsonObject, Iteration obj)
        {
            double targetEstimate;
            DateTime startdate, enddate;
            obj.Id = jsonObject["id"].ReadAs<Int32>();
            obj.Title = jsonObject["title"].ReadAs<string>();
            obj.StartDate = (jsonObject.ValueOrDefault("startdate").TryReadAs<DateTime>(out startdate)) ? (DateTime?)startdate : null;
            obj.EndDate = (jsonObject.ValueOrDefault("enddate").TryReadAs<DateTime>(out enddate)) ? (DateTime?)enddate : null;
            obj.IterationStatusId = jsonObject["iteration_status_id"].ReadAs<int>();
            obj.TargetEstimate = (jsonObject.ValueOrDefault("target_estimate").TryReadAs<Double>(out targetEstimate)) ? (double?)targetEstimate : null;
            obj.ProjectId = jsonObject["project_id"].ReadAs<int>();
        }

        public override void serializeToJSON(System.Json.JsonValue jsonObject, Iteration obj)
        {
            jsonObject["id"] = obj.Id;
            jsonObject["title"] = obj.Title;
            jsonObject["startdate"] = obj.StartDate;
            jsonObject["enddate"] = obj.EndDate;
            jsonObject["iteration_status_id"] = obj.IterationStatusId;
            jsonObject["target_estimate"] = obj.TargetEstimate;
            jsonObject["project_id"] = obj.ProjectId;
        }

        protected override string JSONName()
        {
            return "iteration";
        }
    }
}
